package com.cc.Dao.Impl;

import com.cc.Dao.BaseDao;
import com.cc.Dao.EmployeeDao;
import com.cc.eneity.Employee;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeDaoImpl extends BaseDao implements EmployeeDao {

    @Override
    public List<Employee> getAll(String empName) {
        List<Employee> employees = new ArrayList<Employee>();
        StringBuffer sb = new StringBuffer();
        sb.append("select * from employee where 1=1");
        if (empName != null && !"".equals(empName)) {
            sb.append(" and empName like '%" + empName + "%'");
        }
        sb.append(" And `start` = 0");
        ResultSet rs = executeQuery(sb.toString(),null);
        if (rs != null) {
            try {
                while (rs.next()){
                    Employee employee = new Employee();
                    employee.setId(rs.getInt("id"));
                    employee.setEmpName(rs.getString("empName"));
                    employee.setDepId(rs.getString("depId"));
                    employee.setPosition(rs.getString("position"));
                    employee.setSalary(rs.getString("salary"));
                    employee.setImgUrl(rs.getString("imgUrl"));
                    employee.setStart(rs.getString("start"));
                    employees.add(employee);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                close(rs);
            }

        }
        return employees;
    }

    @Override
    public int add(Employee employee) {
        PreparedStatement ps = null;
        int row;
        String sql = "INSERT INTO employee " +
                "name,depLd,position,start_date,imgUrl,start,birthday values" +
                "(?,?,?,?,?,?,?)";
        try {
            row = ps.executeUpdate(sql);
            Object [] params = {employee.getId(),employee.getDepId(),employee.getPosition(),employee.getStartDate(),employee.getImgUrl(),employee.getStart(),employee.getBirthday()};
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return row;
    }
}
